#!/bin/sh

eXit() {
	echo .
	exit $1
}

#test -e /var/etc/.lcd-weather || eXit 1
test -e /tmp/.lcd4linux || eXit 1


DATA_DIR=/tmp/lcd
DATA_XML=/tmp/weather.xml

if [ -e /var/tuxbox/config/tuxwetter/tuxwetter.conf ]; then
	DATA_LOC=$(grep -m1 "Stadt=" /var/tuxbox/config/tuxwetter/tuxwetter.conf | cut -d= -f2 | cut -d, -f2,3)
fi

test -z $DATA_LOC && eXit 1
test -d $DATA_DIR || mkdir -p $DATA_DIR

wget -q -O $DATA_XML "http://api.weatherunlocked.com/api/forecast/53.042705,-8.823598?app_id=6ab13e0b&app_key=dc1d3cxxxxxxxxxxxxxxxxxxxxxxxxx&lang=de&format=xml"

if [ -e $DATA_XML ]; then
91405
	code=$(sed -e 's/<\/wx_code>/<\/wx_code>\n/g' $DATA_XML | \
			sed -n 's/.*<wx_code>\(.*\)<\/wx_code>.*/\1/p')
			
	text=$(sed -e 's/<\/wx_desc>/<\/wx_desc>\n/g' $DATA_XML | \
			sed -n 's/.*<wx_desc>\(.*\)<\/wx_desc>.*/\1/p')		
	
	update=$(sed -e 's/<\/last_updated>/<\/last_updated>\n/g' $DATA_XML | \
			sed -n 's/.*<last_updated>\(.*\)<\/last_updated>.*/\1/p')

	tempmax=$(sed -e 's/<\/temp_max_c>/<\/temp_max_c>\n/g' $DATA_XML | \
			sed -n 's/.*<temp_max_c>\(.*\)<\/temp_max_c>.*/\1/p')

	tempmin=$(sed -e 's/<\/temp_min_c>/<\/temp_min_c>\n/g' $DATA_XML | \
			sed -n 's/.*<temp_min_c>\(.*\)<\/temp_min_c>.*/\1/p')

	temp=$(sed -n 's/.*<temp_c>\(.*\)<\/temp_c>.*/\1\n/p' $DATA_XML)
	
	wind=$(sed -n 's/.*<windgst_max_kmh>\(.*\)<\/windgst_max_kmh>.*/\1\n/p' $DATA_XML)
	
	regen=$(sed -n 's/.*<rain_total_mm>\(.*\)<\/rain_total_mm>.*/\1\n/p' $DATA_XML)
	
	sunrise=$(sed -n 's/.*<sunrise_time>\(.*\)<\/sunrise_time>.*/\1\n/p' $DATA_XML)
	
	sunset=$(sed -n 's/.*<sunset_time>\(.*\)<\/sunset_time>.*/\1\n/p' $DATA_XML)

	windkmh=$(sed -e 's/<\/windspd_max_kmh>/<\/windspd_max_kmh>\n/g' $DATA_XML | \
			sed -n 's/.*<windspd_max_kmh>\(.*\)<\/windspd_max_kmh>.*/\1/p')
			
	regenmm=$(sed -e 's/<\/totalprecip_mm>/<\/totalprecip_mm>\n/g' $DATA_XML | \
	        sed -n 's/.*<totalprecip_mm>\(.*\)<\/totalprecip_mm>.*/\1/p')

	winddir=$(sed -e 's/<\/winddir_compass>/<\/winddir_compass>\n/g' $DATA_XML | \
			sed -n 's/.*<winddir_compass>\(.*\)<\/winddir_compass>.*/\1/p')					

	feuchte=$(sed -n 's/.*<humidity>\(.*\)<\/humidity>.*/\1\n/p' $DATA_XML)
	
	gust=$(sed -e 's/<\/WindGustKmph>/<\/WindGustKmph>\n/g' $DATA_XML | \
			sed -n 's/.*<WindGustKmph>\(.*\)<\/WindGustKmph>.*/\1/p')	
	
	icon=$(sed -e 's/</WX_icon>/<\/WX_icon>\n/g' $DATA_XML | \
			sed -n 's/.*<WX_icon>\(.*\)<\/WX_icon>.*/\1/p')	
	
	chill=$(sed -e 's/<\/feelslike_c>/<\/feelslike_c>\n/g' $DATA_XML | \
			sed -n 's/.*<feelslike_c>\(.*\)<\/feelslike_c>.*/\1/p')
	
	date=$(sed -e 's/<\/date>/<\/date>\n/g' $DATA_XML | \
			sed -n 's/.*<date>\(.*\)<\/date>.*/\1/p')

	iconurls=$(sed -e 's/<\/icon>/<\/icon>\n/g' $DATA_XML | \
			sed -n 's/.*<icon>\(.*\)<\/icon>.*/\1/p' | \
			sed -e 's/<!\[CDATA\[//g' -e 's/\]\]>//g')
			 url=http://cdn.apixu.com/weather/64x64/day/302.png
		echo${url##*}   

	rm -f $DATA_DIR/weather_*

	echo "$code"		>> $DATA_DIR/weather_code
	echo "$update"		>> $DATA_DIR/weather_update
	echo "$tempmax"		>> $DATA_DIR/weather_temperatures_max
	echo "$tempmin"		>> $DATA_DIR/weather_temperatures_min
	echo "$iconurls"	>> $DATA_DIR/weather_url
	echo "$gust"	    >> $DATA_DIR/weather_gust
	echo "$windkmh"		 > $DATA_DIR/weather_windkmh
	echo "$wind"		 > $DATA_DIR/weather_wind
	echo "$regenmm"      > $DATA_DIR/weather_regenmm
	echo "$regen"        > $DATA_DIR/weather_regen
	echo "$winddir"		 > $DATA_DIR/weather_winddir
	echo "$feuchte"		 > $DATA_DIR/weather_luftfeuchte
	echo "$text"		>> $DATA_DIR/weather_text
	echo "$icon"	    >> $DATA_DIR/weather_icon
	echo "$sunrise"		 > $DATA_DIR/weather_sunrise
	echo "$sunset"		 > $DATA_DIR/weather_sunset
	echo "$chill"		>> $DATA_DIR/weather_chill
	echo "$druck"		 > $DATA_DIR/weather_titel
	echo "$temp"	     > $DATA_DIR/weather_temp

	for url in $iconurls; do
		icon=${url##*/}
		echo ${icon:8:4} >> $DATA_DIR/weather_icon
	done

	#TODO: fix lan/lon-entrys in $DATA_DIR/location
	#http://maps.google.com/maps/geo?output=xml&oe=utf8&ll=(LAT),(LON)&key=asdad&hl=de

	rm -f $DATA_XML
fi

eXit 0

